<template>
  <el-container class="main-container">
    <el-header>
      <img src="../assets/images/logo.png" alt="" />
      <el-menu
        mode="horizontal"
        background-color="#23262e"
        text-color="white"
        active-text-color="rgb(64,158,255)"
        default-active="2"
        unique-opened
      >
        <el-submenu index="2">
          <template slot="title">
            <img class="img-pic" :src="user.user_pic" v-if="user.user_pic" alt="" />
            <img class="img-pic" src="../assets/logo.png" v-else alt="" />
            个人中心</template
          >
          <el-menu-item index="2-1"><i class="el-icon-edit"></i>用户资料</el-menu-item>
          <el-menu-item index="2-2"><i class="el-icon-edit"></i>重置密码</el-menu-item>
          <el-menu-item index="2-2"><i class="el-icon-edit"></i>更换头像</el-menu-item>
        </el-submenu>

        <el-menu-item index="2-4-1"
          ><i class="el-icon-switch-button"></i>
          <span slot="title" @click="logout">退出</span>
        </el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <div class="user-info">
          <img class="img-pic" :src="user.user_pic" v-if="user.user_pic" alt="" />
          <img class="img-pic" src="../assets/logo.png" v-else alt="" />
          欢迎你<span> {{ user.nickname || user.username }}</span>
        </div>

        <el-menu
          background-color="#23262e"
          text-color="white"
          active-text-color="rgb(64,158,255)"
          default-active="1"
          unique-opened
        >
          <template v-for="item in menus">
            <el-menu-item
              :index="item.indexPath"
              v-if="item.children === null"
              :key="item.indexPath"
              ><i :class="item.icon"></i>{{ item.title }}</el-menu-item
            >
            <el-submenu :index="item.indexPath" v-else :key="item.indexPath">
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <el-menu-item
                :index="subItem.indexPath"
                v-for="subItem in item.children"
                :key="subItem.indexPath"
                >{{ subItem.title }}</el-menu-item
              >
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-main>Main</el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenuAPI } from '@/api/menu'
export default {
  name: 'MyHome',
  data() {
    return {
      menus: []
    }
  },
  async created() {
    this.$store.dispatch('user/getUser')
    const { data: res } = await getMenuAPI()
    if (res.code === 0) {
      this.menus = res.data
    }
  },
  computed: {
    ...mapState('user/', ['user'])
  },
  methods: {
    logout() {
      console.log(1)
      // 询问是否要退出？如果确定退出，清除token、user，跳转到登录页
      this.$confirm('你确定要退出吗？', '提示', {
        confirmButtonText: '确定', // 配置确认按钮的文字，默认是确定
        cancelButtonText: '取消', // 配置取消按钮的文字，默认是取消
        type: 'warning' // 弹框的类型，警告框
      })
        .then(() => {
          // 点击了确认进入这里
          this.$store.commit('user/updateToken', '')
          this.$store.commit('user/updateUser', {})
          this.$router.push('/login')
        })
        .catch(() => {
          // 点击了取消，进入这里
        })
    }
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
}
.img-pic {
  width: 35px;
  height: 35px;
  background-color: #fff;
  border-radius: 50%;
  margin-right: 15px;
  // object-fit: cover 和 background-size基本一个意思
  // 表示让图片按照原始的比例完整的显示出来
  object-fit: cover;
}
.user-info {
  display: flex;
  justify-content: center;
  height: 70px;
  align-items: center;
  font-size: 13px;
  color: white;
}
.el-header {
  background-color: #23262e;
  display: flex;
  justify-content: space-between;
}
.el-aside {
  background-color: #23262e;
}
</style>
